﻿Public Class MASPSTATUS
    Dim PROVIDER_Check As New PROVIDERSCLASS
    Dim connect As CONDB = CONDB.NewConnection()
    Dim dtsetMaspstatus As DataSet
    Dim s2 As FILTERCLASS

    Private Sub filterClinic()
        Dim s As FILTERCLASS
        Dim sql As String = "SELECT CAST(clinic AS CHAR(6)) AS clinic, clinicname, cliniccode FROM masclinic WHERE status = '1'"
        s = New FILTERCLASS(txtClinic, sql, "idแผนก,แผนก,codeแผนก", "80,200,50", "1,1,1", "1,1,1")
        's.SetShowInDown = False
        s.SetShowBorder = True
    End Sub

    Public Sub queryMaspstatus()
        dtsetMaspstatus = New DTSETMASPSTATUS

        Dim objGetMaspstatus As New MASPSTATUSCLASS
        Dim sql As String = objGetMaspstatus.selectMaspstatus_
        connect.GetTable(sql, dtsetMaspstatus.Tables("maspstatusJoinMasclinic"))

        connect.Dispose()

        xgvMaspstatus.DataSource = dtsetMaspstatus
        xgvMaspstatus.ForceInitialize()
    End Sub

    Private Sub SROOMITEM_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        queryMaspstatus()

        filterClinic()

        txtPstatus.Text = makeNewPstatusID()
    
        PROVIDER_Check.GETHOSEM(CInt(main.txtUserRequest.Text.Trim), Convert.ToString(Me.Tag))
    End Sub

    Private Sub GridView1_RowClick(sender As Object, e As DevExpress.XtraGrid.Views.Grid.RowClickEventArgs) Handles GridView1.RowClick
        txtPstatus.Text = GridView1.GetDataRow(GridView1.FocusedRowHandle)("pstatus").ToString()
        txtClinic.Tag = GridView1.GetDataRow(GridView1.FocusedRowHandle)("clinic").ToString()
        txtClinic.Text = GridView1.GetDataRow(GridView1.FocusedRowHandle)("clinicname").ToString()
        txtPs_name.Text = GridView1.GetDataRow(GridView1.FocusedRowHandle)("ps_name").ToString()

        If GridView1.GetDataRow(GridView1.FocusedRowHandle)("status") IsNot DBNull.Value Then
            ckbStatus.Checked = Convert.ToBoolean(GridView1.GetDataRow(GridView1.FocusedRowHandle)("status"))
        Else
            ckbStatus.Checked = False
        End If

        If GridView1.GetDataRow(GridView1.FocusedRowHandle)("f_cvn") IsNot DBNull.Value Then
            ckbF_cvn.Checked = Convert.ToBoolean(GridView1.GetDataRow(GridView1.FocusedRowHandle)("f_cvn"))
        Else
            ckbF_cvn.Checked = False
        End If

        If Convert.ToInt64(GridView1.GetDataRow(GridView1.FocusedRowHandle)("pstatus")) <= 20 Then
            txtPs_name.Enabled = False
            txtClinic.Enabled = False
            btnRequestID.Enabled = False
            ckbF_cvn.Enabled = False
            ckbStatus.Enabled = False
            btnEditData.Enabled = False
        Else
            txtPs_name.Enabled = True
            txtClinic.Enabled = True
            btnRequestID.Enabled = True
            ckbF_cvn.Enabled = True
            ckbStatus.Enabled = True
            btnEditData.Enabled = True
        End If
    End Sub

    Private Sub btnAdddata_Click(sender As Object, e As EventArgs) Handles btnAdddata.Click
        If main.PROVIDERPOLICIES.checklogin_user_ = True Then
            If PROVIDER_Check.PADD_ = True Then
                If txtClinic.Tag IsNot Nothing And txtClinic.Tag <> "" And txtPstatus.Text.Length.ToString > 0 And txtPs_name.Text.Length.ToString > 0 Then

                    Dim objAddMaspstatus As New MASPSTATUSCLASS
                    objAddMaspstatus.PSTATUS_ = txtPstatus.Text
                    objAddMaspstatus.PS_NAME_ = txtPs_name.Text.Trim
                    objAddMaspstatus.CLINIC_ = txtClinic.Tag
                    objAddMaspstatus.STATUS_ = ckbStatus.Checked
                    objAddMaspstatus.F_CVN_ = ckbF_cvn.Checked

                    If objAddMaspstatus.insertMaspstatus = True Then
                        queryMaspstatus()
                        clearForm()
                    End If
                Else
                    MsgBox("ท่านต้องระบุข้อมูล ชื่อสถานะการรักษา, คลินิก")
                End If
            Else
                MsgBox(PROVIDER_Check.PADDMSG_)
            End If
        Else
            TOASTCLASS.gloadToastMSG(Me, main.PROVIDERPOLICIES.LOGINUSERMSG_)
            main.txtUserRequest.Focus()
        End If
    End Sub

    Private Sub btnEditData_Click(sender As Object, e As EventArgs) Handles btnEditData.Click
        If main.PROVIDERPOLICIES.checklogin_user_ = True Then
            If PROVIDER_Check.PUPD_ = True Then
                Dim editStat As Integer
                editStat = MsgBox("ท่านต้องการแก้ไขข้อมูลห้องพักผู้ป่วยดังกล่าวใช่หรือไม่", MsgBoxStyle.OkCancel)

                If editStat = 1 Then
                    If txtClinic.Tag IsNot Nothing And txtClinic.Tag <> "" And txtPstatus.Text.Length.ToString > 0 And txtPs_name.Text.Length.ToString > 0 Then

                        If Convert.ToInt16(txtPstatus.Text) > 20 Then
                            Dim objEditMaspstatus As New MASPSTATUSCLASS
                            objEditMaspstatus.PSTATUS_ = txtPstatus.Text
                            objEditMaspstatus.PS_NAME_ = txtPs_name.Text.Trim
                            objEditMaspstatus.CLINIC_ = txtClinic.Tag
                            objEditMaspstatus.STATUS_ = ckbStatus.Checked
                            objEditMaspstatus.F_CVN_ = ckbF_cvn.Checked

                            If objEditMaspstatus.editMaspstatus = True Then
                                queryMaspstatus()
                                clearForm()
                            End If
                        Else
                            MsgBox("ไม่สามารถแก้ไขสถานะการบริการนี้ได้ เนื่องจากเป็นค่ามาตรฐานของระบบ")
                        End If
                    Else
                        MsgBox("ท่านต้องระบุข้อมูล ชื่อสถานะการรักษา, คลินิก")
                    End If
                End If
            Else
                MsgBox(PROVIDER_Check.PUPDMSG_)
            End If
        Else
            TOASTCLASS.gloadToastMSG(Me, main.PROVIDERPOLICIES.LOGINUSERMSG_)
            main.txtUserRequest.Focus()
        End If
    End Sub

    Private Sub importExel_Click(sender As Object, e As EventArgs) Handles importExel.Click
        Dim nextform As New IMPORTEXCEL("sroomitem")
        nextform.ShowDialog()
    End Sub

    Private Sub exportExcel_Click(sender As Object, e As EventArgs) Handles exportExcel.Click
        Dim nextform As New IMPORTEXCEL("sroomitem")
        nextform.ShowDialog()
    End Sub

    Private Sub clearForm()
        txtClinic.Tag = ""
        txtClinic.Clear()
        ckbStatus.Checked = False
        ckbF_cvn.Checked = False
        txtPs_name.Text = ""
        txtPstatus.Text = makeNewPstatusID()
    End Sub

    Private Function makeNewPstatusID() As String
        Dim dt As DataTable
        dt = connect.GetTable("SELECT MAX(pstatus) FROM maspstatus")

        Dim newPstatusID = Convert.ToInt64(dt.Rows(0)(0)) + 1

        Return newPstatusID
    End Function

    Private Sub btnRequestID_Click(sender As Object, e As EventArgs) Handles btnRequestID.Click
        txtPstatus.Text = makeNewPstatusID()
    End Sub
End Class